package com.gec.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.javabean.Ebook;
import com.gec.req.EbookQueryReq;
import com.gec.req.EbookSaveReq;
import com.gec.resp.CommonResp;
import com.gec.resp.EbookQueryResp;
import com.gec.resp.PageResp;
import com.gec.service.EbookService;
import com.gec.utils.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Autowired
    private EbookService ebookService;
    //打印日志
    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    //    @GetMapping("/getEbookList")
//    public List<Ebook> getEbooks() {
//        List<Ebook> list = ebookService.list();
//        for (Ebook ebook : list) {
//            System.out.println(ebook.toString());
//        }
//        return list;
//    }
    @GetMapping("/getEbookList")
    public CommonResp getEbookList() {
        List<Ebook> list = ebookService.list();
        for (Ebook ebook : list) {
            System.out.println(ebook.toString());
        }
        CommonResp<List<Ebook>> resp = new CommonResp<>();
        resp.setContent(list);
        return resp;
    }

    @GetMapping("/getEbookListByName")
    public CommonResp getEbookByName(EbookQueryReq ebookQueryReq) {
        //处理条件查询的一个类
        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        wrapper.like("name", ebookQueryReq.getName());
        List<Ebook> list = ebookService.list(wrapper);

//        ArrayList<EbookResp> ebookResps = new ArrayList<>();
//        for (Ebook ebook : list) {
//            EbookResp copy = CopyUtil.copy(ebook, EbookResp.class);
//            ebookResps.add(copy);
//        }
        List<EbookQueryResp> ebookQueryResps = CopyUtil.copyList(list, EbookQueryResp.class);
        CommonResp<List<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(ebookQueryResps);
        return resp;
    }

    @GetMapping("/getEbookByEbookReq")
    public CommonResp getEbookByEbookReq(EbookQueryReq req) {
        List<Ebook> list;
        System.out.println("req = " + req);
        if (!ObjectUtils.isEmpty(req.getName())) {
            QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
            wrapper.like("name", req.getName());
            list = ebookService.list(wrapper);
        } else {
            list = ebookService.list();
        }
        List<EbookQueryResp> ebookQueryResps = CopyUtil.copyList(list, EbookQueryResp.class);
        CommonResp<List<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(ebookQueryResps);
        return resp;
    }


    @GetMapping("/getEbookListByPage")
    public CommonResp getEbookListByPage(@Valid EbookQueryReq req){
        Page<Ebook> page = new Page<>(req.getPage(), req.getSize());

        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(req.getCategory2_id())){
            wrapper.like("category2_id",req.getCategory2_id());
        }
        if (!ObjectUtils.isEmpty(req.getName())){
            wrapper.like("name",req.getName());
        }

        page = ebookService.page(page,wrapper);
        List<Ebook> list = page.getRecords();

        LOG.info("总行数：{}",page.getTotal()+"");
        LOG.info("总页数：{}",page.getPages()+"");

        List<EbookQueryResp> ebookResps = CopyUtil.copyList(list, EbookQueryResp.class);

        PageResp<EbookQueryResp> respPage = new PageResp<>();
        respPage.setTotal(page.getTotal());
        respPage.setList(ebookResps);

        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(respPage);

        return resp;
    }


    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req){
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        //会根据是否有id判断   修改（有id）  新增（无id）
        ebookService.saveOrUpdate(ebook);

        CommonResp resp = new CommonResp<>();
        return resp;
    }

//    删除
    @GetMapping("/remove")
    public CommonResp remove(int id) {
        ebookService.removeById(id);
        CommonResp<Object> resp = new CommonResp<>();
        return resp;
    }
}


